package com.shouhe.mysap.controller.plan;


import com.shouhe.mysap.common.MySapResult;
import com.shouhe.mysap.common.PageResult;
import com.shouhe.mysap.common.status.PlanStatus;
import com.shouhe.mysap.common.status.PurStatus;
import com.shouhe.mysap.entity.PlanNoSupplier;
import com.shouhe.mysap.entity.PlanPlace;
import com.shouhe.mysap.entity.PurAskPrice;
import com.shouhe.mysap.entity.PurPrice;
import com.shouhe.mysap.service.*;
import com.shouhe.mysap.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 守鹤
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/plan-no-supplier")
public class PlanNoSupplierController {
    @Autowired
    private PlanNoSupplierService planNoSupplierService;

    @Autowired
    private PlanPlaceService planPlaceService;

    @Autowired
    private PurPriceService purPriceService;

    @Autowired
    private PurAskPriceService purAskPriceService;

    /**
     * @描述 得到全部未落实订单
     * @参数
     * @返回值
     */
    @RequestMapping("/getPlanNoSuppliers")
    public PageResult getPlanNoSuppliers(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows) {
        PageResult pageResult = PageResultUtil.getPageResult(page, rows, planNoSupplierService);
        List<?> list = pageResult.getRows();
        for (int i = 0; i < list.size(); i++) {
            PlanNoSupplier noSupplier = (PlanNoSupplier) list.get(i);
            String thingCode = noSupplier.getThingCode();
            List<PurPrice> purPrices = SelectUtil.selectListEq("thing_code", thingCode, purPriceService);
            if (purPrices != null && purPrices.size() > 0) {
                for (PurPrice purPrice : purPrices) {
                    //如果是手动关闭或者已经下单的就不考虑
                    if (noSupplier.getStatus() == PlanStatus.PLAN_NO_SUPPLIER_HAS_PLANPLACE || noSupplier.getStatus() == PlanStatus.PLAN_NO_SUPPLIER_CLOSE_PUR_ORDER) {
                        continue;
                    }
                    //价格财务已经审核，并且是主供应商
                    if (purPrice.getStatus() == 1 && purPrice.getMainSupplier() == 1) {
                        if (noSupplier.getStatus() != PlanStatus.PLAN_NO_SUPPLIER_HAS_PLANPLACE) {
                            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_CANPLACE);
                        }
                        noSupplier.setPurGroup(purPrice.getPurGroup());
                        noSupplier.setSupplierCode(purPrice.getSupplierCode());
                        noSupplier.setSupplierName(purPrice.getSupplierName());
                    } else if (purPrice.getStatus() == 1 && purPrice.getMainSupplier() != 1) {
                        //财务已审核价格，但是这条数据不是主供应商
                        noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_NO_SUPPLIER);
                        noSupplier.setPurGroup(purPrice.getPurGroup());
                    } else if (purPrice.getStatus() == 0 && purPrice.getMainSupplier() == 1) {
                        //财务未审核价格，这条数据是主供应商
                        noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_FINANCE_NOCHECK);
                        noSupplier.setPurGroup(purPrice.getPurGroup());
                        noSupplier.setSupplierCode(purPrice.getSupplierCode());
                        noSupplier.setSupplierName(purPrice.getSupplierName());
                    }
                }
            } else {
                //没有数据
                if (!(noSupplier.getStatus() == PlanStatus.PLAN_NO_SUPPLIER_NO_PURGROUP || noSupplier.getStatus() == PlanStatus.PLAN_NO_SUPPLIER_NO_SUPPLIER || noSupplier.getStatus() == PlanStatus.PLAN_NO_SUPPLIER_HAS_PURGROUP)) {
                    noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_PLAN_CONFIRM);
                }
            }
        }
        List<PlanNoSupplier> planNoSuppliers = ListUtil.getListT(list, PlanNoSupplier.class);
        MySapResult result = UpdateUtil.updateObjectListById("更新PlanNoSupplier的状态", planNoSuppliers, planNoSupplierService);
        if (!result.isSuccess()) {
            return null;
        }
        return pageResult;
    }


    @RequestMapping("/addPlanNoSupplier")
    public MySapResult addPlanNoSupplier(PlanNoSupplier planNoSupplier) {
        return AddUtil.addObject("添加一个供应链询价报表", planNoSupplier, planNoSupplierService);
    }


    /**
     * 将完整的PlanNoSupplier数据添加到PlanPlace中
     *
     * @param ids
     * @return
     */
    @RequestMapping("/addPlanNoSupplierToPlanPlace")
    public MySapResult addPlanNoSupplierToPlanPlace(@RequestParam("ids[]") Integer[] ids) {
        //更新PlanNoSupplier的记录的状态为 已下单
        List<PlanNoSupplier> noSuppliers = SelectUtil.selectListIn("id", Arrays.asList(ids), planNoSupplierService);
        for (PlanNoSupplier noSupplier : noSuppliers) {
            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_HAS_PLANPLACE);
        }
        MySapResult result = UpdateUtil.updateObjectListById("更新供应链未定供应商记录到下单视图", noSuppliers, planNoSupplierService);
        if (!result.isSuccess()) {
            return result;
        }

        //将数据添加到PlanPlace表中
        List<PlanPlace> list2 = new ArrayList<>();

        for (PlanNoSupplier noSupplier : noSuppliers) {
            PlanPlace planPlace = new PlanPlace();
            planPlace.setUnit(noSupplier.getUnit());
            planPlace.setThingName(noSupplier.getThingName());
            planPlace.setThingCode(noSupplier.getThingCode());
            planPlace.setSupplierName(noSupplier.getSupplierName());
            planPlace.setSupplierCode(noSupplier.getSupplierCode());
            planPlace.setStatus(PlanStatus.PLAN_PLACE_CAN_PLANPLACE);
            planPlace.setSaleOrderRow(noSupplier.getSaleOrderRow());
            planPlace.setSaleOrderId(noSupplier.getSaleOrderId());
            planPlace.setPurGroup(noSupplier.getPurGroup());
            planPlace.setNeedTime(noSupplier.getNeedTime());
            planPlace.setNeedCnt(noSupplier.getNeedCnt());
            planPlace.setPlanCnt(noSupplier.getPlanCnt());
            planPlace.setPlanTime(noSupplier.getPlanTime());
            list2.add(planPlace);
        }
        MySapResult result1 = AddUtil.addObjectList("从PlanNoSupplier添加数据到PlanPlace中", list2, planPlaceService);
        return result1;
    }


    /**
     * 将PlanNoSupplier数据,确认采购组
     *
     * @param ids
     * @return
     */
    @RequestMapping("/addPlanNoSupplierToConfirmPurGroup")
    public MySapResult addPlanNoSupplierToConfirmPurGroup(@RequestParam("ids[]") Integer[] ids) {
        //更新PlanNoSupplier的记录的状态为 未确认采购组
        List<PlanNoSupplier> list = new ArrayList<>();
        for (Integer id : ids) {
            PlanNoSupplier noSupplier = new PlanNoSupplier();
            noSupplier.setId(id);
            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_NO_PURGROUP);
            System.out.println(PlanStatus.PLAN_NO_SUPPLIER_NO_PURGROUP + "!!!!!!!!!!!!!!!!!!!!!!!!!");
            list.add(noSupplier);
        }
        MySapResult result = UpdateUtil.updateObjectListById("更新供应链未定供应商记录为未确认采购组", list, planNoSupplierService);
        if (!result.isSuccess()) {
            return result;
        }

        list = SelectUtil.selectListIn("id", Arrays.asList(ids), planNoSupplierService);
        //将数据添加到PurAskPrice表中
        List<PurAskPrice> list2 = new ArrayList<>();

        for (PlanNoSupplier noSupplier : list) {
            PurAskPrice price = new PurAskPrice();
            price.setPlanCnt(noSupplier.getPlanCnt());
            price.setPlanTime(noSupplier.getPlanTime());
            price.setRemark(noSupplier.getRemark());
            price.setStatus(PurStatus.PUR_ASK_PRICE_NO_PURGROUP);
            price.setThingCode(noSupplier.getThingCode());
            price.setThingName(noSupplier.getThingName());
            price.setUnit(noSupplier.getUnit());
            list2.add(price);
        }
        MySapResult result1 = AddUtil.addObjectList("从PlanNoSupplier添加数据到询价报表中", list2, purAskPriceService);
        return result1;
    }


    /**
     * 将PlanNoSupplier数据,确认供应商
     *
     * @param ids
     * @return
     */
    @RequestMapping("/addPlanNoSupplierToConfirmSupplier")
    public MySapResult addPlanNoSupplierToConfirmSupplier(@RequestParam("ids[]") Integer[] ids) {
        //更新PlanNoSupplier的记录的状态为 未确认供应商
        List<PlanNoSupplier> list = new ArrayList<>();
        for (Integer id : ids) {
            PlanNoSupplier noSupplier = new PlanNoSupplier();
            noSupplier.setId(id);
            noSupplier.setStatus(PlanStatus.PLAN_NO_SUPPLIER_NO_SUPPLIER);
            list.add(noSupplier);
        }
        MySapResult result = UpdateUtil.updateObjectListById("更新供应链未定供应商记录为未确认供应商", list, planNoSupplierService);
        if (!result.isSuccess()) {
            return result;
        }

        //更新PurAskPrice表中的数据为未确认供应商
        List<PlanNoSupplier> planNoSuppliers = SelectUtil.selectListIn("id", Arrays.asList(ids), planNoSupplierService);
        List<String> thingCodes = new ArrayList<>();
        for (PlanNoSupplier planNoSupplier : planNoSuppliers) {
            thingCodes.add(planNoSupplier.getThingCode());
        }

        List<PurAskPrice> purAskPrices = SelectUtil.selectListIn("thing_code", thingCodes, purAskPriceService);

        for (PurAskPrice purAskPrice : purAskPrices) {
            purAskPrice.setStatus(PurStatus.PUR_ASK_PRICE_NO_SUPPLIER);
        }
        MySapResult result1 = UpdateUtil.updateObjectListById("更新询价报价中的状态为未定供应商", purAskPrices, purAskPriceService);

        if (!result1.isSuccess()) {
            return result1;
        }

        return result1;
    }

    @RequestMapping("/updatePlanNoSupplier")
    public MySapResult updatePlanNoSupplier(PlanNoSupplier planNoSupplier) {
        return UpdateUtil.updateObjectById("更新供应链询价报表", planNoSupplier, planNoSupplierService);
    }

    @RequestMapping("/deletePlanNoSuppliers")
    public MySapResult deletePlanNoSuppliers(@RequestParam("ids[]") Integer[] ids) {
        return DeleteUtil.deleteMany("删除供应链询价报表", ids, planNoSupplierService);
    }


}

